package com.bwda.dsrs.systemservice.controller;

import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.form.msgtemplate.MsgTemplateCreateForm;
import com.bwda.dsrs.systemservice.domain.form.msgtemplate.MsgTemplateQueryForm;
import com.bwda.dsrs.systemservice.domain.form.msgtemplate.MsgTemplateUpdateForm;
import com.bwda.dsrs.systemservice.domain.po.msgtemplate.MsgTemplatePo;
import com.bwda.dsrs.systemservice.domain.condition.msgtemplate.MsgTemplateCondition;
import com.bwda.dsrs.systemservice.domain.vo.msgtemplate.MsgTemplateVo;
import com.bwda.dsrs.systemservice.service.MsgTemplateService;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;

import java.util.List;
import java.util.ArrayList;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 邮件
 *
 * @author yj
 * @date 2019年4月28日
 */
@RestController
@RequestMapping("/s/msgtemplate")
@Api(value = "/s/msgtemplate", tags = "邮件短信模板表")
public class MsgTemplateController extends BaseController {

	@Autowired
	private MsgTemplateService msgTemplateService;
	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询邮件短信模板表",notes = "查询邮件短信模板表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<MsgTemplateVo> query(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws SystemServiceException {
		MsgTemplatePo po = msgTemplateService.queryWithValid(id);
		MsgTemplateVo vo = CopyUtil.transfer(po, MsgTemplateVo.class);
		return getSuccessResult(vo);
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询邮件短信模板表数量",notes = "查询邮件短信模板表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid MsgTemplateQueryForm form) throws SystemServiceException {
		MsgTemplateCondition condition = this.getConditionByQueryForm(form);
		int count = msgTemplateService.queryCount(condition);
		return getSuccessResult(count);
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询邮件短信模板表列表",notes = "查询邮件短信模板表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<MsgTemplateVo>> queryList(@ModelAttribute@Valid MsgTemplateQueryForm form) throws SystemServiceException {
		MsgTemplateCondition condition = this.getConditionByQueryForm(form);
		List<MsgTemplatePo> poList = msgTemplateService.queryList(condition);
		List<MsgTemplateVo> voList = CopyUtil.transfer(poList, MsgTemplateVo.class);
		return getSuccessResult(getListResponse(voList));
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询邮件短信模板表列表(带分页)",notes = "查询邮件短信模板表列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询邮件短信模板表列表(带分页)", type = LogTypeEnum.QUERY, include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<MsgTemplateVo>> queryPageList(@ModelAttribute@Valid MsgTemplateQueryForm form) throws SystemServiceException {
		MsgTemplateCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(Constants.NOT_DELETED);
		List<MsgTemplateVo> voList = new ArrayList<>();
		int count = msgTemplateService.queryCount(condition);
		if (count > 0) {
			List<MsgTemplatePo> poList = msgTemplateService.queryList(condition);
			voList = CopyUtil.transfer(poList, MsgTemplateVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "新增邮件短信模板表",notes = "新增邮件短信模板表",httpMethod = "POST")
	@Loggable(descp = "新增邮件短信模板表", type = LogTypeEnum.CREATE, include = "")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<MsgTemplateVo> add(@ModelAttribute@Valid MsgTemplateCreateForm form) throws SystemServiceException {
		MsgTemplatePo po = CopyUtil.transfer(form, MsgTemplatePo.class);
		msgTemplateService.insert(po);
		MsgTemplateVo vo = CopyUtil.transfer(po, MsgTemplateVo.class);
		return getSuccessResult(vo);
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "修改邮件短信模板表",notes = "修改邮件短信模板表",httpMethod = "POST")
	@Loggable(descp = "修改邮件短信模板表", type = LogTypeEnum.UPDATE, include = "")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid MsgTemplateUpdateForm form) throws SystemServiceException {
		MsgTemplatePo po = CopyUtil.transfer(form, MsgTemplatePo.class);
		msgTemplateService.update(po);
		return getSuccessResult();
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "删除邮件短信模板表",notes = "删除邮件短信模板表",httpMethod = "POST")
	@Loggable(descp = "删除邮件短信模板表", type = LogTypeEnum.DELETE, include = "")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam List<Integer> id) throws SystemServiceException {
		if (id == null || id.size() == 0) {
			return getFailResult("参数错误");
		}
		msgTemplateService.batchDelete(id);
		return getSuccessResult();
	}

	/**
	 * MsgTemplateQueryForm转换为MsgTemplateCondition
	 *
	 * @param form
	 * @return
	 */
	private MsgTemplateCondition getConditionByQueryForm(MsgTemplateQueryForm form) {
		MsgTemplateCondition condition = CopyUtil.transfer(form, MsgTemplateCondition.class);
		return condition;
	}

}